<?php
require_once("conf.php");
//客户端类
class CLIENT{
	// private $link;	//数据库连接

	public function __construct(){
		// global $DB_HOST;	
		// global $DB_PORT;
		// global $DB_NAME;
		// global $DB_USER;
		// global $DB_PWD;
		// global $DB_CHARSET;
		// //连接远程数据库
	 //    $this->link = @mysql_connect($DB_HOST.':'.$DB_PORT, $DB_USER, $DB_PWD);
	 //    //选择数据库
	 //    mysql_select_db($DB_NAME, $this->link);
	 //    //选择字符集
	 //    mysql_set_charset($DB_CHARSET);
	}

	/*
	 * 服务端注册
	 * @param string $username 用户名
	 * @param string $password 密码,MD5加密
	 * @param string $source 注册来源
	 * @return string 成功返回服务端id，失败返回-1，用户名重复返回0
	 
	function sys_regist($username,$password,$source){
		global $DB_PREFIX;

		//数据库连接失败
		if(!$this->link){
			return -1;
		}
		//查询用户名是否重复
		$sql = "select id from ".$DB_PREFIX."user where username = '".$username."'";
		@$re = mysql_query($sql);
		@$row = mysql_fetch_assoc($re);
		//用户名重复
		if($row){
			return 0;
		}
		//添加新用户
		$sql = "insert into ".$DB_PREFIX."user (username,password,source)values('{$username}','{$password}','{$source}')";
		@$re = mysql_query($sql);
		if($re){
			return @mysql_insert_id();
		}else{
			return -1;
		}
	}
	*/

	/**
	 * 服务端注册2
	 * @param string $info 用户信息
	 * @return string 成功返回服务端id，失败返回-1，用户名重复返回0
	 */
	function sys_regist($info){
		global $HOST_SS;		//服务端生成用户信息缓存地址
        $url = $HOST_SS."regist";
        // return $url;
        $curl=curl_init();
		curl_setopt ( $curl, CURLOPT_URL, $url);
		curl_setopt ( $curl, CURLOPT_POST, 1 );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $info );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒
		$value=curl_exec($curl);
		curl_close($curl);
	    return $value;
	}

	/*
	 * 服务器登录
	 * @param string $username 用户名
	 * @param string $password 密码,MD5加密
	 * @return string 成功返回用户信息，连接失败返回-1，用户或密码错误返回0
	 
	function sys_login($username,$password){
		global $DB_PREFIX;	//表前缀
		global $DB_FIELD;		//可在配置文件中设置需要返回的字段
		global $HOST_SS;		//服务端生成用户信息缓存地址
		//数据库连接失败
		if(!$this->link){
			return -1;
		}
	
		//验证登录信息
		$sql = "select ".$DB_FIELD." from ".$DB_PREFIX."user where username = '".$username."' and password ='".$password."'";
		@$re = mysql_query($sql);
		@$row = mysql_fetch_assoc($re);
		if($row){
			//生成标识
            $bs = md5(time().mt_rand(1000,9999));
            while($this->checkBs($bs)){
                $bs = md5(time().mt_rand(1000,9999));
            }
            $this->SS($bs,$row['id']);
            $url = $HOST_SS."setUserInfo/bs/".$bs;
            // return $url;
        	$curl = curl_init ();
	        curl_setopt ( $curl, CURLOPT_URL, $url );
	        curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
	        $value = curl_exec ( $curl );
	        curl_close ( $curl );
			return $value;	//成功返回iframe
		}else{
			return 0;		//用户或密码错误
		}
	}
	*/
	/**
	 * 服务端登录
	 * @param string $info 用户信息
	 * @return string 成功返回iframe
	 */
	function sys_login($info){
		global $HOST_SS;		//服务端生成用户信息缓存地址
        $url = $HOST_SS."setUserInfo";
        // return $url;
        $curl=curl_init();
		curl_setopt ( $curl, CURLOPT_URL, $url);
		curl_setopt ( $curl, CURLOPT_POST, 1 );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $info );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒
		$value=curl_exec($curl);
		curl_close($curl);
	    return $value;
	}
	/**
	 *	退出触发事件
	 */
	function sys_logout(){
		global $HOST_SS;		//服务端生成用户信息缓存地址
        $url = $HOST_SS."logout";
        // return $url;
        $curl = curl_init ();
	    curl_setopt ( $curl, CURLOPT_URL, $url );
	    curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒

	    $value = curl_exec ( $curl );
	    curl_close ( $curl );
	    return $value;
	}

	/**
	 * 修改用户信息
	 * @param array $info,以字段为下标的用户信息，（要包括修改的的用户所属uid）
	 * @return boolean 成功返回true,失败返回false
	 */
	function sys_update($info){
		global $HOST_SS;		//服务端生成用户信息缓存地址
		$bs = md5(time().mt_rand(1000,9999));
        while($this->checkBs($bs)){
            $bs = md5(time().mt_rand(1000,9999));
        }
        $this->SS($bs,$info);
		$url=$HOST_SS."modifyInfo/bs/".$bs;
		$curl=curl_init();
		curl_setopt ( $curl, CURLOPT_URL, $url);
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $curl, CURLOPT_TIMEOUT, 2);   //设置超时时间为2秒
		
		$value = curl_exec ( $curl );
	    curl_close ( $curl );
		return $value;
	}
	// //关闭数据库连接
	// function close(){
	// 	return mysql_close($this->link);
	// }

	//检测缓存标识是否存在
    function checkBs($bs){
        if($this->SS($bs)){
            return 1;
        }else{
            return 0;
        }
    }
    /** 用户信息缓存
	 * @param string $key,缓存名
	 * @param string $val,用户信息
	 * @param int $time,生命周期
	 * @return 存值无返回，取值返回值，删除返回1，错误返回0，无法连接返回Could not connect
	 */
	function SS($key,$val='',$time){
	    if(empty($key)){
	        return 0;
	    }
	    //获取配置文件memcache配置
	    global $MEM_HOST;
	    global $MEM_PORT;
	    global $MEM_TIME;
        $key = 'user'.$key;
	    $mem = new \Memcache();
	    $mem->connect($MEM_HOST, $MEM_PORT) or die ("Could not connect");
	    if($val === ''){ // 获取缓存
            return json_decode($mem->get($key),true);
	        // return $mem->get($key);
	    }elseif(is_null($val)) { // 删除缓存
	        return $mem->delete($key);
	    }
	    //有传$val,没传$time，读配置过期时间
	    if(empty($time)){
	        $time = $MEM_TIME;
	    }
	    //存值
        $val = json_encode($val);
	    @$mem->set($key,$val,0,$time);
	    $mem->close();
	}

    //获取没有绑定手机邮箱登录的账号
    public function bangding($type,$username){
    	global $HOST_SS;
        $users=file_get_contents($HOST_SS."getusers/t/$type/u/$username");
        $user=json_decode($users,true);
        return $user;
    }

    //提交认证信息
    public function auth($data,$fun){
    	global $HOST_SS;
        $curl=curl_init();
		curl_setopt ( $curl, CURLOPT_URL, $HOST_SS.$fun);
		curl_setopt ( $curl, CURLOPT_POST, 1 );
		curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
		curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
		$value=curl_exec($curl);
		curl_close($curl);
	    return $value;
    }

    //获取认证信息
    public function getauth($uid){	
    	global $HOST_SS;
    	$auth=file_get_contents($HOST_SS."get_auth/u_id/$uid");
        return json_decode($auth,true);
    }

    //获取是否存在已认证其他账号
    public function alreadyauth($data,$type){	
    	global $HOST_SS;
    	$re=file_get_contents($HOST_SS."alreadyauth/sno/$data/type/$type");
        return $re;
    } 

    //开启手机号邮箱登录
    public function openlogin($type,$uid){	
    	global $HOST_SS;
    	$re=file_get_contents($HOST_SS."openlogin/type/$type/uid/$uid");
        return $re;
    }        

    //获取用户信息
    public function getuserinfos($uid){	
    	global $HOST_SS;
    	$re=file_get_contents($HOST_SS."getuserinfos/uid/$uid");
        return json_decode($re,true);
    }
}

	/*远程服务器注册例子，将这段代码加入子站的注册方法里，根据实际进行修改
    require_once("UCenter/client.php");
    $client = new \CLIENT();
    $re = $client->sys_regist($_POST['username'],md5($_POST['password']),'国内站');
    //注册失败
    if($re == -1){
        $this->error("注册失败", U('Index/reg'));
    }
    if($re == 0){
        $this->error("用户名重复", U('Index/reg'));
    }*/